/*
 * 文件名：ArticletypeServiceImpl.java	 
 * 时     间：上午11:26:48
 * 作     者：chenyi       
 * 版     权： 2014-2022  智慧园区, 公司保留所有权利.
 * 联     系：www.szyungu.com
 */
package com.qianhai.news.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qianhai.common.dao.face.BaseMapper;
import com.qianhai.common.entity.LoginUserInfo;
import com.qianhai.common.exception.BaseException;
import com.qianhai.common.service.impl.BaseServiceImpl;
import com.qianhai.common.utils.DateUtil;
import com.qianhai.common.utils.ShiroUtil;
import com.qianhai.common.utils.Tools;
import com.qianhai.common.utils.UUIDGenerateUtil;
import com.qianhai.news.dao.mapers.ArticletypeMapper;
import com.qianhai.news.model.Articletype;
import com.qianhai.news.model.ArticletypeExample;
import com.qianhai.news.page.ArticletypePage;
import com.qianhai.news.service.face.ArticletypeService;
import com.qianhai.news.vo.ArticleTypeVo;

/** 
 * @ClassName: ArticletypeServiceImpl 
 * @Description: 这里用一句话描述这个类的作用 
 * @author chenyi
 * @date 2014年11月14日 上午11:26:48 
 */
@Service
public class ArticletypeServiceImpl extends BaseServiceImpl<ArticletypeExample, Articletype, String> implements ArticletypeService
{


	@Autowired
	private ArticletypeMapper map;
	
	@Override
	public BaseMapper<ArticletypeExample, Articletype, String> getMapper()
	{
		// TODO Auto-generated method stub
		return map;
	}
	
	
	public int save(Articletype atype){
		try
		{

			LoginUserInfo user = ShiroUtil.getCurrentUser();
			if (null == atype.getArticletypeName())
			{
				throw new BaseException("50000-00000-0012");
			} else if (null == atype.getArticletypeState())
			{
				throw new BaseException("50000-00000-0013");
			} else if (null == atype.getArticletypeType())
			{
				throw new BaseException("50000-00000-0014");
			} else if (null == atype.getArticletypeTypecn())
			{
				throw new BaseException("50000-00000-0015");
			}  else if (null ==user)
			{
				throw new BaseException("10000-00003");
			} 
			if(Tools.isEmpty(atype.getArticletypeCode())){
				atype.setArticletypeCode(UUIDGenerateUtil.generate());
			}
			atype.setUserUuid(user.getUuid());
			atype.setArticletypeUuid(UUIDGenerateUtil.generate());
			atype.setArticletypeCreatetime(DateUtil.getNowTimeStr());
			return map.insertSelective(atype);
		} catch (Throwable e)
		{
			log.error(e.getMessage(), e);
			throw new BaseException(e.getMessage());
		}
	}

	public int update(Articletype atype){
		try
		{
			
			if (null == atype.getArticletypeUuid())
			{
				throw new BaseException("50000-00000-0016");
			}
			return map.updateByPrimaryKeySelective(atype);
		} catch (Throwable e)
		{
			log.error(e.getMessage(), e);
			throw new BaseException(e.getMessage());
		}
		
	}
	

	public List<ArticleTypeVo> queryTypeListArticle(ArticletypePage<Articletype> page){
		try
		{
			
			List<Articletype> list = map.queryTypeTree(page);
			List<ArticleTypeVo> listvo = new ArrayList<ArticleTypeVo>();
			if (list != null && list.size() > 0)
			{
				for (int i = 0; i < list.size(); i++)
				{
					getListByFatherId(page,list.get(i));
				}
				listvo = setVo( list, listvo);
			}
					return listvo;
		} catch (Throwable e)
		{
			log.error(e.getMessage(), e);
			throw new BaseException(e.getMessage());
		}
		
	}
	
	public void getListByFatherId(ArticletypePage<Articletype> page,Articletype atype)throws BaseException
	{
		try
		{
			page.setFatherId(atype.getArticletypeCode());
			List<Articletype> listchild = map.queryTypeTree(page);
			atype.setList(listchild);
			if(listchild!=null && listchild.size()>0){
				for (Articletype at : listchild)
				{
					getListByFatherId(page,at);
				}
				
			}
			
		} catch (Throwable e)
		{
			throw new BaseException(e.getMessage());
		}
	}
	
	public List<ArticleTypeVo> setVo(List<Articletype> list,List<ArticleTypeVo> listvo){
		//List<ArticleTypeVo> listvo = new ArrayList<ArticleTypeVo>();
		for (int i = 0; i < list.size(); i++)
		{
			ArticleTypeVo vo = new ArticleTypeVo();
			Articletype atype = list.get(i);
			
			ArticletypeExample ate = new ArticletypeExample();
			ate.or().andArticletypeCodeEqualTo(atype.getArticletypeCode());
			List<Articletype> listcode = map.selectByCondition(ate);
			
			for (int j = 0; j < listcode.size(); j++)
			{
				if(listcode.get(j).getArticletypeLanguage().equals("zh_CN")){//中文
					vo.setArticletypeName(listcode.get(j).getArticletypeName());
				}else if(listcode.get(j).getArticletypeLanguage().equals("en_US")){//英文
					vo.setArticletypeNameEn(listcode.get(j).getArticletypeName());
				}else if(listcode.get(j).getArticletypeLanguage().equals("pt-br")){//葡文
					vo.setArticletypeNamePt(listcode.get(j).getArticletypeName());
				}
			}
			vo.setArticletypeCode(atype.getArticletypeCode());
			vo.setArticletypeType(atype.getArticletypeType());
			vo.setArticletypeTypecn(atype.getArticletypeTypecn());
			List<ArticleTypeVo> listvochild = new ArrayList<ArticleTypeVo>();
			vo.setList(listvochild);
			if(atype.getList()!=null && atype.getList().size()>0){
				setVo(atype.getList(),vo.getList());
			}
			listvo.add(vo);
		}
		
		return listvo;
		
	}


	@Override
	public List<Articletype> queryTypeLanguageTree(ArticletypePage<Articletype> page)
	{
		try
		{
			
			List<Articletype> list = map.queryTypeTree(page);
			if (list != null && list.size() > 0)
			{
				for (int i = 0; i < list.size(); i++)
				{
					getListByFatherId(page,list.get(i));
				}
			}
					return list;
		} catch (Throwable e)
		{
			log.error(e.getMessage(), e);
			throw new BaseException(e.getMessage());
		}
	}
	
}
